package com.tonghe.dao.bill;

import com.mysql.cj.util.StringUtils;
import com.tonghe.dao.BaseDao;
import com.tonghe.pojo.Bill;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date:2021/12/12- 12- 12- 13:41
 **/
public class BillDaoImpl implements BillDao {
    @Override
    public List<Bill> getBillList(Connection connection, Bill bill) {
        // TODO Auto-generated method stub
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Bill> billList = new ArrayList<Bill>();
        if(connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select b.*,p.proName as providerName from smbms_bill b, smbms_provider p where b.providerId = p.id");
            List<Object> list = new ArrayList<Object>();
            if(!StringUtils.isNullOrEmpty(bill.getProductName())){
                sql.append(" and productName like ?");
                list.add("%"+bill.getProductName()+"%");
            }
            if(bill.getProviderId() > 0){
                sql.append(" and providerId = ?");
                list.add(bill.getProviderId());
            }
            if(bill.getIsPayment() > 0){
                sql.append(" and isPayment = ?");
                list.add(bill.getIsPayment());
            }
            Object[] params = list.toArray();
            System.out.println("sql --------- > " + sql.toString());
            try {
                rs = BaseDao.execute(connection, pstm,rs,sql.toString(),params);
                while(rs.next()){
                    Bill _bill = new Bill();
                    _bill.setId(rs.getInt("id"));
                    _bill.setBillCode(rs.getString("billCode"));
                    _bill.setProductName(rs.getString("productName"));
                    _bill.setProductDesc(rs.getString("productDesc"));
                    _bill.setProductUnit(rs.getString("productUnit"));
                    _bill.setProductCount(rs.getBigDecimal("productCount"));
                    _bill.setTotalPrice(rs.getBigDecimal("totalPrice"));
                    _bill.setIsPayment(rs.getInt("isPayment"));
                    _bill.setProviderId(rs.getInt("providerId"));
                    _bill.setProviderName(rs.getString("providerName"));
                    _bill.setCreationDate(rs.getTimestamp("creationDate"));
                    _bill.setCreatedBy(rs.getInt("createdBy"));
                    billList.add(_bill);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(null, pstm, rs);
            }
        }
        return billList;
    }
}
